package cn.iocoder.yudao.module.member.service.bankaccountinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.bankaccountinfo.vo.BankAccountInfoPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.bankaccountinfo.vo.BankAccountInfoSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.bankaccountinfo.BankAccountInfoDO;
import cn.iocoder.yudao.module.member.dal.mysql.bankaccountinfo.BankAccountInfoMapper;
import cn.iocoder.yudao.module.member.utils.AuthIdentityUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.BANK_ACCOUNT_INFO_DUPLICATE;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.BANK_ACCOUNT_INFO_NOT_EXISTS;

/**
 * 用户转账信息 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BankAccountInfoServiceImpl implements BankAccountInfoService {

    @Resource
    private BankAccountInfoMapper bankAccountInfoMapper;
    @Resource
    private AuthIdentityUtil authIdentityUtil;

    @Override
    public Long createBankAccountInfo(Long loginUserId, BankAccountInfoSaveReqVO createReqVO) {

        // TODO: 2025/4/26 银行卡有效信息核验
        long count = bankAccountInfoMapper.selectCount(BankAccountInfoDO::getBankId, createReqVO.getBankId());
        if (count > 1) {
            throw exception(BANK_ACCOUNT_INFO_DUPLICATE);
        }

        // 插入
        BankAccountInfoDO bankAccountInfo = BeanUtils.toBean(createReqVO, BankAccountInfoDO.class);
        BankAccountInfoDO bankAccountInfoDO = bankAccountInfoMapper.selectOne(BankAccountInfoDO::getBankId, createReqVO.getBankId());
        if (bankAccountInfoDO != null) {
            bankAccountInfo.setId(bankAccountInfoDO.getId());
        }
        bankAccountInfo.setUserId(loginUserId);
        bankAccountInfoMapper.insertOrUpdate(bankAccountInfo);
        // 返回
        return bankAccountInfo.getId();
    }

    @Override
    public void updateBankAccountInfo(BankAccountInfoSaveReqVO updateReqVO) {
        long count = bankAccountInfoMapper.selectCount(BankAccountInfoDO::getBankId, updateReqVO.getBankId());
        if (count > 1) {
            throw exception(BANK_ACCOUNT_INFO_DUPLICATE);
        }
        // 校验存在
        validateBankAccountInfoExists(updateReqVO.getId());
        // 更新
        BankAccountInfoDO updateObj = BeanUtils.toBean(updateReqVO, BankAccountInfoDO.class);
        bankAccountInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteBankAccountInfo(Long id) {
        // 校验存在
        validateBankAccountInfoExists(id);
        // 删除
        bankAccountInfoMapper.deleteById(id);
    }

    private void validateBankAccountInfoExists(Long id) {
        if (bankAccountInfoMapper.selectById(id) == null) {
            throw exception(BANK_ACCOUNT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BankAccountInfoDO getBankAccountInfo(Long id) {
        return bankAccountInfoMapper.selectById(id);
    }

    @Override
    public PageResult<BankAccountInfoDO> getBankAccountInfoPage(BankAccountInfoPageReqVO pageReqVO) {
        return bankAccountInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BankAccountInfoDO> getBankAccountInfoList(Long loginUserId) {
        return bankAccountInfoMapper.selectListByUserId(loginUserId);
    }

    @Override
    public void createBankAccount(Long userId, String name, String bankId, String bankName, String bankAddress) {
        BankAccountInfoSaveReqVO req = new BankAccountInfoSaveReqVO();
        req.setName(name);
        req.setBankId(bankId);
        req.setBankName(bankName);
        req.setBankAddress(bankAddress);
        req.setDefaultStatus(true);
        createBankAccountInfo(userId, req);
    }


}